import { Inject, Injectable } from '@nestjs/common';
import { ChatHistory } from '@prisma/client';
import { PrismaService } from '@/module/prisma/prisma.service';
import { ResultData } from '@/common/util/result';

export type HistoryDto = Pick<ChatHistory, 'chatroomId' | 'senderId' | 'type' | 'content'>;
@Injectable()
export class ChatHistoryService {
    @Inject()
    private prismaService: PrismaService;

    async list(chatroomId: number) {
        const history = await this.prismaService.chatHistory.findMany({
            where: {
                chatroomId
            }
        });
        const res = [];
        for (let i = 0; i < history.length; i++) {
            const user = await this.prismaService.user.findUnique({
                where: {
                    id: history[i].senderId
                },
                select: {
                    id: true,
                    username: true,
                    nickName: true,
                    email: true,
                    createTime: true,
                    headPic: true
                }
            });
            res.push({
                ...history[i],
                sender: user
            });
        }
        return ResultData.ok(res)
    }

    async add(chatroomId: number, history: HistoryDto) {
        return await this.prismaService.chatHistory.create({
            data: history
        })
    }
}
